package com.lzw.breezeNovel.webApp.service.impl.message;

import com.lzw.breezeNovel.common.exception.BaseException;
import com.lzw.breezeNovel.common.login.LoginUser;
import com.lzw.breezeNovel.common.login.LoginUserHolder;
import com.lzw.breezeNovel.common.result.ResultCodeEnum;
import com.lzw.breezeNovel.model.entity.pojo.message.Message;
import com.lzw.breezeNovel.model.entity.pojo.user.UserInfo;
import com.lzw.breezeNovel.model.enums.message.MessageStatus;
import com.lzw.breezeNovel.webApp.mapper.message.MessageMapper;
import com.lzw.breezeNovel.webApp.mapper.user.UserInfoMapper;
import com.lzw.breezeNovel.webApp.service.message.MessageService;
import com.lzw.breezeNovel.webApp.vo.message.MessageVO;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service.impl.message
 * @date 2025/3/28
 * @description 消息
 */
@Service
@SuppressWarnings("all")
public class MessageServiceImpl implements MessageService {

   /**
    * 注入mapper
    */
   @Resource
   private MessageMapper messageMapper;
    /**
     * 注入当前用户mapper
     */
    @Resource
    private UserInfoMapper userInfoMapper;

      /**
       * 获取所有的消息
       * @param senderId
       * @param receiverId
       * @return
       */
      @Override
      public List<MessageVO> getMessageList(Long senderId) {
       //教育参数
      if (senderId == null) {
         return List.of();
      }
      //获取当前用户
      Long userId = getUserId();
      //查询数据库
      List<Message> messageList = messageMapper.getMessageList(senderId, userId);
       //拷贝对象
       return copyMessageList(messageList);
      }

        /**
         * 添加消息
         * @param senderId
         * @param message
        */
        @Override
        public void addMessage(Long receiverId, Long userId,String content, Integer type) {
            //校验参数
           if (receiverId == null || content == null) {
              return;
           }
           //创建消息对象
           Message message = new Message();
           message.setSenderId(userId);
           //设置接收者
           message.setReceiverId(receiverId);
           //设置内容
           message.setContent(content);
           //设置消息的类型
           message.setType(type);
           //设置状态
           message.setStatus(MessageStatus.UNREAD.getCode());
           //设置创建时间
           message.setCreateTime(new Date());
           //设置更新时间
           message.setUpdateTime(new Date());
           //设置发送时间
           message.setSendTime(new Date());
           //保存到数据库
           messageMapper.insert(message);
        }

    /**
     * 删除消息
     * @param messageId
     */
    @Override
    public void deleteMessage(Long messageId) {
        //校验参数
        if (messageId == null) {
            return;
        }
        messageMapper.deleteMessage(messageId);
    }

    /**
     * 获取用户发过来的消息
     * @return
     */
    @Override
    public List getMessage() {
        //获取当前用户
        Long userId = getUserId();
        //查询数据库
        List<Message> messageList = messageMapper.getCurrent(userId);
        //拷贝对象
        List<MessageVO> messageVOList = copyMessageList(messageList);
        return messageVOList;
    }

    @Nullable
    @Contract(pure = true)
    private Long getUserId() {
        //获取当前用户
        LoginUser loginUser = LoginUserHolder.getLoginUser();
       //判断是否登入
       if (loginUser == null) {
           throw  new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
       }
       //获取手机号
       String phone = loginUser.getPhone();
       //查询数据库
       UserInfo userInfo = userInfoMapper.selectByPhone(phone);
       //判断数据是否存在
       if (userInfo == null) {
          throw new BaseException(ResultCodeEnum.APP_LOGIN_AUTH);
       }
       //返回用户id
       return userInfo.getUserId();
    }

    /**
     * 拷贝对象
     */
    private List<MessageVO> copyMessageList(List<Message> messageList) {
       //判断数据是否为空
       if (messageList == null || messageList.isEmpty()) {
          return List.of();
       }
       return messageList.stream().map(message -> {
          MessageVO messageVO = new MessageVO();
          BeanUtils.copyProperties(message, messageVO);
          //获取状态
         Integer type = message.getStatus();
         //根据状态获取数据
          String statusDesc = MessageStatus.getEnumByCode(type);
          messageVO.setStatus(statusDesc);
          //设置发送的对象
           messageVO.setSendUser(message.getUserInfo());
          return messageVO;
       }).collect(Collectors.toList());
    }
}
